home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / X / Xserver / layerdemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  6.4 KB  |  169 lines

  1. /*
  2.  * Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /* $Revision: 1.6 $ */
  18. /* compile: cc -o layerdemo layerdemo.c XLayerUtil.c -lX11 -lm */
  19.  
  20. /* Tested on Silicon Graphics IRIX 4.0 and IRIX 5.0 workstations and
  21.  * Hewlett-Packard workstations with CRX24 and CRX48Z graphics hardware.
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include <math.h>
  26. #include "XLayerUtil.h"
  27.  
  28. #define SIZE 400 /* width and height of window */
  29.  
  30. Display *dpy;
  31. Window root, win, overlay;
  32. Colormap cmap;
  33. Visual *defaultVisual;
  34. int screen, black, white, red, nVisuals, i, status;
  35. GC normalGC, overlayGC;
  36. XEvent event;
  37. XLayerVisualInfo template;
  38. XLayerVisualInfo *otherLayerInfo, *defaultLayerInfo;
  39. XSetWindowAttributes winattrs;
  40. XGCValues gcvals;
  41. XColor color, exact;
  42. int x = 0, y = SIZE/2;
  43.  
  44. redrawNormalPlanes()
  45. {
  46.    /* draw a black 43 legged octopus */
  47.    for(i=0; i<43; i++)
  48.       XDrawLine(dpy, win, normalGC, SIZE/2, SIZE/2,
  49.          (int) (cos(i * 0.15) * (SIZE/2-5)) + SIZE/2,
  50.      (int) (sin(i * 0.15) * (SIZE/2-12)) + SIZE/2);
  51. }
  52.  
  53. #define MESSAGE1 "This text is in the"
  54. #define MESSAGE2 "OVERLAY PLANES"
  55.  
  56. redrawOverlayPlanes()
  57. {
  58.    XDrawString(dpy, overlay, overlayGC, x, y, MESSAGE1, sizeof(MESSAGE1)-1);
  59.    XDrawString(dpy, overlay, overlayGC, x, y + 15, MESSAGE2, sizeof(MESSAGE2)-1);
  60. }
  61.  
  62. fatalError(message)
  63. char *message;
  64. {
  65.    fprintf(stderr, "layerdemo: %s\n", message);
  66.    exit(1);
  67. }
  68.  
  69. main(argc, argv)
  70. int argc;
  71. char *argv[];
  72. {
  73.    dpy = XOpenDisplay(NULL);
  74.    if(dpy == NULL) fatalError("cannot open display");
  75.    screen = DefaultScreen(dpy);
  76.    root = RootWindow(dpy, screen);
  77.    defaultVisual = DefaultVisual(dpy, screen);
  78.    /* find layer of default visual */
  79.    template.vinfo.visualid = defaultVisual->visualid; 
  80.    defaultLayerInfo = XGetLayerVisualInfo(dpy, VisualIDMask, &template, &nVisuals);
  81.    /* look for visual in layer "above" default visual with transparent pixel */
  82.    template.layer = defaultLayerInfo->layer + 1;
  83.    template.vinfo.screen = screen;
  84.    template.type = TransparentPixel;
  85.    otherLayerInfo = XGetLayerVisualInfo(dpy,
  86.       VisualScreenMask|VisualLayerMask|VisualTransparentType, &template, &nVisuals);
  87.    if(otherLayerInfo == NULL) {
  88.       /* make sure default visual has transparent pixel */
  89.       if(defaultLayerInfo->type == None) fatalError("unable to find expected layer visuals");
  90.       /* visual not found "above" default visual, try looking "below" */
  91.       template.layer = defaultLayerInfo->layer - 1;
  92.       template.vinfo.screen = screen;
  93.       otherLayerInfo = XGetLayerVisualInfo(dpy,
  94.      VisualScreenMask|VisualLayerMask, &template, &nVisuals);
  95.       if(otherLayerInfo == NULL) fatalError("unable to find layer below default visual");
  96.       /* XCreateColormap uses AllocNone for 2 reasons:
  97.        * 1) haven't determined class of visual, visual could have static colormap
  98.        * and more importantly
  99.        * 2) transparent pixel might make AllocAll impossible.
  100.        */
  101.       cmap = XCreateColormap(dpy, root, otherLayerInfo->vinfo.visual, AllocNone);
  102.       /* not default colormap, must find our own black and white */
  103.       status = XAllocNamedColor(dpy, cmap, "black", &color, &exact);
  104.       if(status == 0) fatalError("could not allocate black");
  105.       black = color.pixel;
  106.       status = XAllocNamedColor(dpy, cmap, "white", &color, &exact);
  107.       if(status == 0) fatalError("could not allocate white");
  108.       white = color.pixel;
  109.       winattrs.background_pixel = white;
  110.       winattrs.border_pixel = black;
  111.       winattrs.colormap = cmap;
  112.       win = XCreateWindow(dpy, root, 10, 10, SIZE, SIZE, 0, otherLayerInfo->vinfo.depth,
  113.      InputOutput, otherLayerInfo->vinfo.visual,
  114.      CWBackPixel|CWBorderPixel|CWColormap, &winattrs);
  115.       status = XAllocNamedColor(dpy, DefaultColormap(dpy, screen),
  116.          "red", &color, &exact);
  117.       if(status == 0) fatalError("could not allocate red");
  118.       winattrs.background_pixel = defaultLayerInfo->value;
  119.       winattrs.border_pixel = 0;
  120.       winattrs.colormap = DefaultColormap(dpy, screen);
  121.       overlay = XCreateWindow(dpy, win, 0, 0, SIZE, SIZE, 0, DefaultDepth(dpy, screen),
  122.      InputOutput, defaultVisual, CWBackPixel|CWBorderPixel|CWColormap, &winattrs);
  123.    } else {
  124.       /* create lower window using default visual */
  125.       black = BlackPixel(dpy, screen);
  126.       white = WhitePixel(dpy, screen);
  127.       win = XCreateSimpleWindow(dpy, root, 10, 10, SIZE, SIZE, 1, black, white);
  128.       /* see note above about AllocNone */
  129.       cmap = XCreateColormap(dpy, root, otherLayerInfo->vinfo.visual, AllocNone);
  130.       status = XAllocNamedColor(dpy, cmap, "red", &color, &exact);
  131.       if(status == 0) fatalError("could not allocate red");
  132.       red = color.pixel;
  133.       winattrs.background_pixel = otherLayerInfo->value; /* use transparent pixel */
  134.       winattrs.border_pixel = 0; /* no border but still necessary to avoid BadMatch */
  135.       winattrs.colormap = cmap;
  136.       overlay = XCreateWindow(dpy, win, 0, 0, SIZE, SIZE, 0, otherLayerInfo->vinfo.depth,
  137.      InputOutput, otherLayerInfo->vinfo.visual,
  138.      CWBackPixel|CWBorderPixel|CWColormap, &winattrs);
  139.    }
  140.    XSelectInput(dpy, win, ExposureMask);
  141.    XSelectInput(dpy, overlay, ExposureMask|ButtonPressMask);
  142.    XSetWMColormapWindows(dpy, win, &overlay, 1);
  143.    gcvals.foreground = black;
  144.    gcvals.line_width = 8;
  145.    gcvals.cap_style = CapRound;
  146.    normalGC = XCreateGC(dpy, win, GCForeground|GCLineWidth|GCCapStyle, &gcvals);
  147.    gcvals.foreground = red;
  148.    overlayGC = XCreateGC(dpy, overlay, GCForeground, &gcvals);
  149.    XMapSubwindows(dpy, win);
  150.    XMapWindow(dpy, win);
  151.    while(1) {
  152.       XNextEvent(dpy, &event);
  153.       switch(event.type) {
  154.       case Expose:
  155.          if(event.xexpose.window == win)
  156.             redrawNormalPlanes();
  157.          else
  158.             redrawOverlayPlanes();
  159.          break;
  160.       case ButtonPress:
  161.      x = random() % SIZE/2;
  162.      y = random() % SIZE;
  163.      XClearWindow(dpy, overlay);
  164.          redrawOverlayPlanes();
  165.          break;
  166.       }
  167.    }
  168. }
  169.